package controller

import (
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"gin-vue-admin/model/response"
	"gin-vue-admin/service"
	"gin-vue-admin/utils"
	"sort"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

// GetDeptList
// @Tags SysDept
// @Summary 查询部门列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.QueryDept true " "
// @success 200 {object} response.Response{data=[]model.SysDept} "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /dept/getDeptList [get]
func GetDeptList(c *gin.Context) {
	var dept request.QueryDept
	_ = c.ShouldBindQuery(&dept)

	data, err := service.GetDeptInfoList(dept)
	list := data.GetRootTree()
	sort.Sort(list)
	list.SyntheticTree(data)

	response.RespondData(list, err, c)
}

// CreateDept
// @Tags SysDept
// @Summary 新增部门
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysDept true " "
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /dept/addDept [post]
func CreateDept(c *gin.Context) {
	var dept model.SysDept
	_ = c.ShouldBindJSON(&dept)
	if err := utils.Verify(dept); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	claims, _ := c.Get("claims")
	user := claims.(*request.CustomClaims)
	dept.CreateTime = time.Now()
	dept.UpdateTime = time.Now()
	dept.CreateBy = user.Username
	dept.UpdateBy = user.Username

	err := service.CreateDept(dept)
	response.RespondOperate(err, c)
}

// UpdateDept
// @Tags SysDept
// @Summary 更新部门
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysDept true " "
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /dept/updateDept [put]
func UpdateDept(c *gin.Context) {
	var dept model.SysDept
	_ = c.ShouldBindJSON(&dept)
	if err := utils.Verify(dept); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	claims, _ := c.Get("claims")
	user := claims.(*request.CustomClaims)
	dept.UpdateTime = time.Now()
	dept.UpdateBy = user.Username

	err := service.UpdateDept(dept)
	response.RespondOperate(err, c)
}

// QueryDept
// @Tags SysDept
// @Summary 查询部门信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param id path int true " "
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /dept/{id} [get]
func QueryDept(c *gin.Context) {
	deptId, _ := strconv.Atoi(c.Param("id"))
	if deptId == 0 {
		response.FailWithMessage("id不能为空", c)
	}

	dept, err := service.QueryDept(uint64(deptId))
	response.RespondData(dept, err, c)
}

// DeleteDept
// @Tags SysDept
// @Summary 批量删除部门
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true " "
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /dept/deleteDept [delete]
func DeleteDept(c *gin.Context) {
	deptId := c.Param("deptId")
	if deptId == "" {
		response.FailWithMessage("deptId不能为空", c)
		return
	}

	err := service.DeleteDept(deptId)
	response.RespondOperate(err, c)
}
